﻿Imports System
Imports System.Collections.Generic
Imports System.ComponentModel
Imports System.ComponentModel.Design
Imports System.Drawing
Imports System.Workflow.ComponentModel
Imports System.Workflow.ComponentModel.Compiler

Namespace System.Workflow.Activities
    <ToolboxBitmap(GetType(IfElseActivity), "Resources.Decision.png"), SRDescription("ConditionalActivityDescription"), ActivityValidator(GetType(IfElseValidator)), SRCategory("Standard"), ToolboxItem(GetType(IfElseToolboxItem)), Designer(GetType(IfElseDesigner), GetType(IDesigner))> _
    Public NotInheritable Class IfElseActivity
        Inherits CompositeActivity
        Implements IActivityEventListener([Of] ActivityExecutionStatusChangedEventArgs)
        ' Methods
        Public Sub New()
        End Sub

        Public Sub New(ByVal name As String)
            MyBase.New(name)
        End Sub

        Public Function AddBranch(ByVal activities As ICollection([Of] Activity)) As IfElseBranchActivity
            If (activities Is Nothing) Then
                Throw New ArgumentNullException("activities")
            End If
            Return Me.AddBranch(activities, Nothing)
        End Function

        Public Function AddBranch(ByVal activities As ICollection([Of] Activity), ByVal branchCondition As ActivityCondition) As IfElseBranchActivity
            If (activities Is Nothing) Then
                Throw New ArgumentNullException("activities")
            End If
            If Not MyBase.DesignMode Then
                Throw New InvalidOperationException(SR.GetString("Error_ConditionalBranchUpdateAtRuntime"))
            End If
            Dim item As New IfElseBranchActivity
            Dim activity2 As Activity
            For Each activity2 In activities
                item.Activities.Add(activity2)
            Next
            item.Condition = branchCondition
            MyBase.Activities.Add(item)
            Return item
        End Function

        Protected Overrides Function Cancel(ByVal executionContext As ActivityExecutionContext) As ActivityExecutionStatus
            Dim flag As Boolean = True
            Dim i As Integer
            For i = 0 To MyBase.EnabledActivities.Count - 1
                Dim activity As Activity = MyBase.EnabledActivities.Item(i)
                If (activity.ExecutionStatus = ActivityExecutionStatus.Executing) Then
                    flag = False
                    executionContext.CancelActivity(activity)
                    Exit For
                End If
                If ((activity.ExecutionStatus = ActivityExecutionStatus.Canceling) OrElse (activity.ExecutionStatus = ActivityExecutionStatus.Faulting)) Then
                    flag = False
                    Exit For
                End If
            Next i
            If Not flag Then
                Return ActivityExecutionStatus.Canceling
            End If
            Return ActivityExecutionStatus.Closed
        End Function

        Protected Overrides Function Execute(ByVal executionContext As ActivityExecutionContext) As ActivityExecutionStatus
            If (executionContext Is Nothing) Then
                Throw New ArgumentNullException("executionContext")
            End If
            Dim flag As Boolean = True
            Dim i As Integer
            For i = 0 To MyBase.EnabledActivities.Count - 1
                Dim activity As IfElseBranchActivity = [TryCast](MyBase.EnabledActivities.Item(i),IfElseBranchActivity)
                If ((activity.Condition Is Nothing) OrElse activity.Condition.Evaluate(activity, executionContext)) Then
                    flag = False
                    activity.RegisterForStatusChange(Activity.ClosedEvent, Me)
                    executionContext.ExecuteActivity(activity)
                    Exit For
                End If
            Next i
            If Not flag Then
                Return ActivityExecutionStatus.Executing
            End If
            Return ActivityExecutionStatus.Closed
        End Function

        Private Sub System.Workflow.ComponentModel.IActivityEventListener<System.Workflow.ComponentModel.ActivityExecutionStatusChangedEventArgs>.OnEvent(ByVal sender As Object, ByVal e As ActivityExecutionStatusChangedEventArgs) Implements IActivityEventListener([Of] ActivityExecutionStatusChangedEventArgs).OnEvent
            If (sender Is Nothing) Then
                Throw New ArgumentNullException("sender")
            End If
            If (e Is Nothing) Then
                Throw New ArgumentNullException("e")
            End If
            Dim context As ActivityExecutionContext = [TryCast](sender,ActivityExecutionContext)
            If (context Is Nothing) Then
                Throw New ArgumentException(SR.Error_SenderMustBeActivityExecutionContext, "sender")
            End If
            context.CloseActivity
        End Sub

    End Class
End Namespace

